package badgegenerator.dao;

import badgegenerator.dao.mybatis.MyBatisConnector;
import badgegenerator.dao.mybatis.IBadgeProjectMapper;
import badgegenerator.dao.exceptions.DAOException;
import badgegenerator.dao.exceptions.MyBatisInitException;
import badgegenerator.dao.mybatis.IEmployeeMapper;
import badgegenerator.dao.mybatis.IProjectRecordDTOMapper;
import badgegenerator.dao.mybatis.ProjectRecordDTO;
import badgegenerator.model.BadgeProject;
import badgegenerator.model.Employee;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

public class BadgeProjectDAO {
    public BadgeProject getProjectById(Integer id) throws DAOException{
        SqlSession session = null;
        BadgeProject resultProject = null;
        try{
            session = MyBatisConnector.getSession(false);
            IBadgeProjectMapper mapper = session.getMapper(IBadgeProjectMapper.class);
            resultProject = mapper.getProjectById(id);
        }
        catch (MyBatisInitException ex) {
            throw new DAOException(ex);
        }        finally{
            if(session != null)
                session.close();
        }
        
        return resultProject;
    }
    
    public void saveOrUpdate(BadgeProject project, SqlSession session) throws DAOException{
        saveOrdUpdatePrivate(project, session);
    }
    
    public void saveOrUpdate(BadgeProject project) throws DAOException{
        SqlSession session = null;
        saveOrdUpdatePrivate(project, session);
        if(session != null)
            session.close();
    }
    
    private void saveOrdUpdatePrivate(BadgeProject project, SqlSession session) throws DAOException{
        try{
            session = MyBatisConnector.getSession(false);
            IBadgeProjectMapper projMapper = session.getMapper(IBadgeProjectMapper.class);
            IEmployeeMapper employeeMapper = session.getMapper(IEmployeeMapper.class);
            IProjectRecordDTOMapper prDTOMapper = session.getMapper(IProjectRecordDTOMapper.class);
            
            if(project.getId() == null){
                projMapper.insert(project);
            }
            else{
                projMapper.update(project);
            }
            session.commit();
            
            List<ProjectRecordDTO> prDTOList = new ArrayList<ProjectRecordDTO>();
            for(Employee employee : project.getEmployeeList()){
                if(employee.getId() == null){
                    employeeMapper.insert(employee);
                    prDTOList.add(new ProjectRecordDTO(project.getId(), employee.getId()));
                }
                else{
                    employeeMapper.update(employee);
                }
            }
            session.commit();
            
            for(ProjectRecordDTO dto : prDTOList)
                prDTOMapper.insert(dto);
            session.commit();
        }
        catch (MyBatisInitException ex) {
            throw new DAOException(ex);
        }
    }
    
    public void delete(BadgeProject project) throws DAOException{
        SqlSession session = null;
        try{
            session = MyBatisConnector.getSession(false);
            IBadgeProjectMapper projMapper = session.getMapper(IBadgeProjectMapper.class);
            IProjectRecordDTOMapper prDTOMapper = session.getMapper(IProjectRecordDTOMapper.class);
            IEmployeeMapper employeeMapper = session.getMapper(IEmployeeMapper.class);
            
            for(int i = 0; i < project.getEmployeeList().size(); i++){
                ProjectRecordDTO prDTO = new ProjectRecordDTO(project.getId(), project.getEmployeeList().get(i).getId());
                prDTOMapper.delete(prDTO);
                employeeMapper.delete(project.getEmployeeList().get(i));
            }
            
            projMapper.delete(project);
            
            session.commit();
        }
        catch (MyBatisInitException ex) {
            throw new DAOException(ex);
        }        finally{
            if(session != null)
                session.close();
        }
    }
}
